package com.self.hctransfer.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;


public class POIServiceUtil {
	  /**
	   * 获取word内容
	   * @param path
	   * @return
	   */
	  public static String readWord(String path) {
	      String buffer = "";
	      try {
	          if (path.endsWith(".doc")) {
	              InputStream is = new FileInputStream(new File(path));
	              WordExtractor ex = new WordExtractor(is);
	              buffer = ex.getText();
	              ex.close();
	          }
	      } catch (Exception e) {
	          e.printStackTrace();
	      }
	      return buffer;
	  }
	  
	  public static List<String> getWordTitles(String path) throws IOException{
		  String buffer = "";
		  
	      try {
	          if (path.endsWith(".doc")) {
	              InputStream is = new FileInputStream(new File(path));
	              WordExtractor ex = new WordExtractor(is);
//	              ex.getParagraphText();
	              String[] ls = ex.getParagraphText();
	              // i 是做什么的？
//	              int i = 0;
	              for(String l : ls) {
	            	  System.out.println("===========");
	            	  String s1 = l.replace("", "");
	            	  String reg = "( HYPERLINK \"javascript:SLC\\([0-9]+,[0-9]+\\)\" )";
	            	  String reg2 = "\\(\\u200Bjavascript:SLC\\([0-9]+,[0-9]+\\)\\u200B\\)";
	            	  Pattern pat = Pattern.compile(reg);

	            	  Matcher mat=pat.matcher(s1); 
	            	  String repickStr = mat.replaceAll("");
	            	  pat = Pattern.compile(reg2);
	            	  mat = pat.matcher(repickStr);
					  repickStr = mat.replaceAll("");
	            	  repickStr = repickStr.replace("", "");
//	            	  System.out.println(repickStr);
//	            	  if(i > 30) {
//	            		  break;
//	            	  }
//	            	  i++;
	              }
	              buffer = ex.getText();
	              ex.close();
	          }
	      } catch (Exception e) {
	          e.printStackTrace();
	      }
	      return null;
		}
	  public static void main(String[] args) throws IOException {
		  getWordTitles("X:\\OnlineDocTransfer\\compare1.doc");
	}
}
